import { ref, reactive, computed } from "vue"
import { toast } from "~/composables/util"
import {getType} from '~/api/home'
import { useStore } from 'vuex'
import { getStorage} from '~/composables/storage'

export function useInitTable(opt = {}) {
    const store = useStore()
    let userType =  getStorage('userType')
    let userMessage =  getStorage('userMessage')


    let searchForm = null
    let resetSearchForm = null
    if (opt.searchForm) {
        searchForm = reactive({ ...opt.searchForm })
        resetSearchForm = () => {
            for (const key in opt.searchForm) {
                searchForm[key] = opt.searchForm[key]
            }
            getData()
        }
    }

    const otherParams = ref({})
    const tableData = ref([])
    const loading = ref(false)

    // 分页
    const currentPage = ref(1)
    const total = ref(10)
    const limit = ref(10)


    if(opt.limit){
        limit.value = opt.limit
    }
    // 获取数据
    function getData(p = null) {

        if (typeof p == "number") {
            currentPage.value = p
        }
        if(opt.getList){
            loading.value = true
            

            let params = {
                index:currentPage.value,
                size:limit.value,
                ...otherParams.value,
                ...searchForm
            }
            if(opt.isUser){
                let userId = ''
                if(userType == 1){
                    userId = userMessage.orgId
                  }else if(userType == 2){
                    if(userMessage&&userMessage.userId){
                        userId = userMessage.userId
                    }
                  }else{
                    userId = null
                  }
                params.userid = userId
                params.usertype = userType || null
            }
            opt.getList(params)
                .then(res => {
                    if (opt.onGetListSuccess && typeof opt.onGetListSuccess == "function") {
                        opt.onGetListSuccess(res)
                    } else {
                        if(res.data.result){
                            tableData.value = res.data.result
                        }else{
                            tableData.value = []
                        }
                        total.value = res.data.page.total
                    }
                })
                .finally(() => {
                    loading.value = false
                })
        }
    }


    if(!opt.isFirst){
        getData()
    }


    const  options = ref([{
        label: "全部",
        value: ""
    }])
    const getTypeList =()=>{
        getType({type:opt.type}).then(res=>{
            if(res&&res.code == '0000'){
                if(opt.isNoAll){
                    options.value = res.data.result.dictList
                }else{
                    if(opt.type == 4){
                        options.value = [{
                            label: "全部资讯",
                            value: ""
                        }]
                    }
                    if(opt.type == 3){
                        options.value = [{
                            label: "全部分类",
                            value: ""
                        }]
                    }
                    options.value = [...options.value,...res.data.result.dictList]
                }
                
            }
        })
    }
    if(opt.type){
        getTypeList()
    }

    // 下拉值改变
    const onUpdate = (e, option) => {
        searchForm.type = e
        currentPage.value = 1
        getData()
      }
 
    //   工具函数过滤
      const filterType = (type,options)=>{
          if(options.length==0){
            return ''
          }
        let item = options.find(v=>v.value == type)
        return item?.label
      }



    return {
        searchForm,
        resetSearchForm,
        tableData,
        loading,
        currentPage,
        total,
        limit,
        getData,
        otherParams,
        options,
        onUpdate,
        filterType
    }
}

